

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.math.*;
//import jv.thirdParty.acmeEncoders.ImageEncoder;
import java.awt.image.ImageConsumer;




import com.sun.corba.se.spi.orbutil.fsm.Input;


import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.imageio.plugins.bmp.BMPImageWriteParam;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;



public class ImageApplet extends Applet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int IDEAL_WIDTH = 680;
	public static final int IDEAL_HEIGHT = 470;
	public static final int CellSize = 10;

	private JInternalFrame frame = new JInternalFrame();
	
   private int numOfHits = 0;    

	//Scanner scan = new Scanner(System.in);

	int map[][][] = new int[70][50][2];
	int nextShip = 5;

	Image water = loadImage("Images/Water.jpg");
	Image land = loadImage("Images/land.jpg");
	Image bowup = loadImage("Images/BowUp.jpg");
	Image bowdown = loadImage("Images/BowDown.jpg");
	Image bowright = loadImage("Images/BowRight.jpg");
	Image bowleft = loadImage("Images/BowLeft.jpg");
	Image boathoriz = loadImage("Images/BoatHoriz.jpg");
	Image boatvert = loadImage("Images/BoatVert.jpg");
	Image hit = loadImage("Images/Hit.jpg");
	Image miss = loadImage("Images/Miss.jpg");

	private Image loadImage(String name)
	{



		Image result = null;
		MediaTracker tracker = new MediaTracker(this);

		Toolkit toolkit = Toolkit.getDefaultToolkit();

		result = toolkit.getImage(name);
		tracker.addImage(result, 0);
		try
		{
			tracker.waitForAll();
		} catch (InterruptedException e)
		{
			return null;
		}

		return result;
	}
	public ImageApplet()
	{
		super();
		
		for (int i = 0; i < 68; i++)
			for (int j = 0; j < 50; j++)
			{
				map[i][j][1] = 0;
			}
	}


	public void paint(Graphics g)
	{

		//CreateTestMap();
		//AddShip('C','V',10,10);
		//AddShip('C','H',20,20);
		//AddShip('B','V',10,20);
		//AddShip('B','H',20,10);
		//AddShip('S','V',10,30);
		//AddShip('S','H',30,10);
		//AddShip('D','V',10,40);
		//AddShip('D','H',40,10);
		//AddShip('P','V',10,45);
		//AddShip('P','H',45,10);

		int xLoc = 0,
		yLoc = 0;
		String newType = null;
		String newOrient = null;

		

		int type = 0;
		for (int i = 0; i < 68; i++)
			for (int j = 0; j < 50; j++)
			{
				type = map[i][j][1];
				if (type == 0)
					g.drawImage(water, i*10, j*10, null);
				else if (type == 1)
					g.drawImage(land, i*10, j*10, null);
				else if (type == 2)
					g.drawImage(bowup, i*10, j*10, null);
				else if (type == 3)
					g.drawImage(bowdown, i*10, j*10, null);
				else if (type == 4)
					g.drawImage(bowright, i*10, j*10, null);
				else if (type == 5)
					g.drawImage(bowleft, i*10, j*10, null);
				else if (type == 6)
					g.drawImage(boathoriz, i*10, j*10, null);
				else if (type == 7)
					g.drawImage(boatvert, i*10, j*10, null);
				else if (type == 8)
					g.drawImage(hit, i*10, j*10, null);
				else if (type == 9)
					g.drawImage(miss, i*10, j*10, null);
			}
	}



	private void CreateTestMap(){
		for (int i = 0; i < 68; i++)
			for (int j = 0; j < 50; j++)
			{
				map[i][j][1] = 0;
			}

		map[10][10][1] = 2;
		map[10][11][1] = 7;
		map[10][12][1] = 7;
		map[10][13][1] = 8;
		map[10][14][1] = 3;

		map[40][10][1] = 2;
		map[40][11][1] = 7;
		map[40][12][1] = 8;
		map[40][13][1] = 3;

		map[20][20][1] = 5;
		map[21][20][1] = 6;
		map[22][20][1] = 6;
		map[23][20][1] = 4;

		map[30][10][1] = 5;
		map[31][10][1] = 4;

		map[5][5][1] = 9;
		map[10][20][1] = 9;
		map[30][34][1] = 9;
		map[49][49][1] = 9;
		map[1][40][1] = 9;
	}

	public boolean AddShip(char shipType, char align, int x, int y)
	{
		//cycle through ships if not directly called with shipType
		if (shipType == 'N')
		{
		
			switch (nextShip)
			{
			case 5: 
			{
				shipType = 'C';
				nextShip--;
				break;
			}			
			case 4: 
			{
				shipType = 'B';
				nextShip--;
				break;
			}	
			case 3:
			{
				shipType = 'D';
				nextShip--;
				break;
			}	
			case 2: 
			{
				shipType = 'S';
				nextShip--;
				break;
			}	
			case 1:
			{
				shipType = 'P';
				nextShip--;
				break;
			}	
			
			}
		}
		
		
		//Process shipType and change Matrix
		if (shipType == 'C')
		{
			if (align == 'V')
			{
				if ((y + 4)>=(IDEAL_HEIGHT / CellSize))
				{
					nextShip++;
					return false;
				}
					
				map[x][y][1] = 2;
				map[x][y+1][1] = 7;
				map[x][y+2][1] = 7;
				map[x][y+3][1] = 7;
				map[x][y+4][1] = 3;
			}

			if (align == 'H')
			{
				if ((x + 4)>=(IDEAL_WIDTH / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 5;
				map[x+1][y][1] = 6;
				map[x+2][y][1] = 6;
				map[x+3][y][1] = 6;
				map[x+4][y][1] = 4;
			}
		}

		if (shipType == 'B')
		{
			if (align == 'V')
			{
				if ((y + 3)>=(IDEAL_HEIGHT / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 2;
				map[x][y+1][1] = 7;
				map[x][y+2][1] = 7;
				map[x][y+3][1] = 3;
			}

			if (align == 'H')
			{
				if ((x + 3)>=(IDEAL_WIDTH / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 5;
				map[x+1][y][1] = 6;
				map[x+2][y][1] = 6;
				map[x+3][y][1] = 4;
			}
		}

		if (shipType == 'D')
		{
			if (align == 'V')
			{
				if ((y + 2)>=(IDEAL_HEIGHT / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 2;
				map[x][y+1][1] = 7;
				map[x][y+2][1] = 3;
			}

			if (align == 'H')
			{
				if ((x + 2)>=(IDEAL_WIDTH / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 5;
				map[x+1][y][1] = 6;
				map[x+2][y][1] = 4;
			}
		}

		if (shipType == 'S')
		{
			if (align == 'V')
			{
				if ((y + 2)>=(IDEAL_HEIGHT / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 2;
				map[x][y+1][1] = 7;
				map[x][y+2][1] = 3;
			}

			if (align == 'H')
			{
				if ((x + 2)>=(IDEAL_WIDTH / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 5;
				map[x+1][y][1] = 6;
				map[x+2][y][1] = 4;
			}
		}

		if (shipType == 'P')
		{
			if (align == 'V')
			{
				if ((y + 1)>=(IDEAL_HEIGHT / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 2;
				map[x][y+1][1] = 3;
			}

			if (align == 'H')
			{
				if ((x + 1)>=(IDEAL_WIDTH / CellSize))
				{
					nextShip++;
					return false;
				}
				
				map[x][y][1] = 5;
				map[x+1][y][1] = 4;
			}
		}
		return true;
		
	}

    public int hitResult(int x, int y)
    {
	System.out.println("HitResult: Checking " + x + "," + y + "...");

	if ((map[x][y][1] > 2) && (map[x][y][1] < 9))
	    {
		System.out.println(" *** HIT *** (@" + x + "," + y);
		if (map[x][y][1] !=8)
			numOfHits++;
		return 8;
	    }
	else
	    return 9;
    }


    public int fireShot(char ammo, int x, int y)
	{
	    numOfHits = 0;
	    System.out.println("FIRE: " + x + "," + y + " with " + ammo);

	    
		    if (ammo == 'A')
			map[x][y][1] = hitResult(x,y);
			
		    else if (ammo == 'B')
			{
			    map[x][y][1] = hitResult(x,y);
			    if (y+1 < IDEAL_HEIGHT / CellSize)
				map[x][y+1][1] = hitResult(x,y+1);
			    if (y-1 >= 0)
				map[x][y-1][1] = hitResult(x,y-1);
			    if (x+1 < IDEAL_WIDTH / CellSize)
				map[x+1][y][1] = hitResult(x+1,y);
			    if (x-1 >= 0)
				map[x-1][y][1] = hitResult(x-1,y);
				}
		     else if (ammo == 'C')
			{
				map[x][y][1] = hitResult(x,y);
				if (y+1 < IDEAL_HEIGHT / CellSize)
					map[x][y+1][1] = hitResult(x,y+1);
				if (y-1 >= 0)
					map[x][y-1][1] = hitResult(x,y-1);
				if (x+1 < IDEAL_WIDTH / CellSize)
					map[x+1][y][1] = hitResult(x+1,y);
				if (x-1 >= 0)
					map[x-1][y][1] = hitResult(x-1,y);
				if((x+1 < IDEAL_WIDTH / CellSize) && (y+1 <IDEAL_HEIGHT / CellSize))
						map[x+1][y+1][1] = hitResult(x+1,y+1);
				if((x+1 < IDEAL_WIDTH / CellSize) && (y-1 >= 0))
					map[x+1][y-1][1] = hitResult(x+1,y-1);
				if((x-1 >= 0) && (y-1 >= 0))
					map[x-1][y-1][1] = hitResult(x-1,y-1);
				if ((x-1 >= 0) && (y+1 < IDEAL_HEIGHT / CellSize))
					map[x-1][y+1][1] = hitResult(x-1,y+1);
			}
		

	    return numOfHits;
	}


	public void clearX()
	{
		for (int i = 0; i < 68; i++)
			for (int j = 0; j < 50; j++)
			{
				if (map[i][j][1] == 8)
					map[i][j][1] = 0;
			}
	}
	
	public JInternalFrame getFrame()
	{
		return frame;
	}
	
	
	
	
	
	public void Init()
	{
		Applet applet = new ImageApplet();

		frame.add(applet);
		frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
		frame.setVisible(true);



	}
	
	public static void main(String [] args)
	{
		ImageApplet applet = new ImageApplet();
		JInternalFrame frame = applet.getFrame();



		frame.add(applet);
		frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
		frame.setVisible(true);
	}
}